package com.yixun.qfbao.hander;

import com.yixun.qfbao.apidto.GetAgentPartnerPageReqDto;
import com.yixun.qfbao.apidto.GetPartnerInfoByDeptResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Department;
import com.yixun.qfbao.model.dto.PartnerInfoDto;
import com.yixun.qfbao.dao.DepartmentDao;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.vo.GetAgentPartnerPageReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @classname: GetPartnerInfoByDeptHander.java
 * @author: Beau
 * @create: 2019/12/20 15:31
 * @version: 1.0.0
 **/
@Service("getPartnerInfoByDeptHander")
public class GetPartnerInfoByDeptHander extends CommonAbstractHander {

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private PartnerInfoService partnerInfoService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetAgentPartnerPageReqVo reqVo = (GetAgentPartnerPageReqVo) reqDto;
        GetAgentPartnerPageReqDto reqDto1 = reqVo.getParams();
        BaseResponseDTO<GetPartnerInfoByDeptResDto> res = new BaseResponseDTO<>();
        GetPartnerInfoByDeptResDto resDto = new GetPartnerInfoByDeptResDto();
        PartnerInfoDto partnerInfoDto = new PartnerInfoDto();
        Long deptId = reqDto1.getDeptId();
        Department department = departmentDao.selectById(deptId);
        if (department != null) {
            List<Department> departments = departmentDao.listDeptByParentId(department.getMerchantId(),deptId);
            List<Long> deptIds = departments.stream().map(Department::getId).collect(Collectors.toList());
            partnerInfoDto.setDeptIds(deptIds);
            resDto.setPartnerList(partnerInfoService.selectListByDto(partnerInfoDto));
            res.setResData(resDto);
            res.setResult(ResultEnum.SUCCESS.getNo());
        }
        return res;
    }
}
